﻿<!DOCTYPE html>
<html lang="zh-cn" class="fullscreen-bg" xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <!--全局通用框架样式 begin-->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
    <!--/*@thymesVar id="title" type=""*/-->
    <title>轮播图</title>
    <link href="/coral/images/favicon.ico" rel="icon">
    <link href="/assets/libs/layui/css/layui.css" rel="stylesheet"/>
    <link href="/assets/module/admin.css" rel="stylesheet">

    <link href="//cdn.bootcss.com/font-awesome/4.2.0/css/font-awesome.min.css" rel="stylesheet">
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
    <script type="text/javascript" src="/assets/libs/layui/layui.js"></script>
    <script type="text/javascript" src="/assets/js/common.js"></script>
    <script type="text/javascript" src="/coral/js/custom.js"></script>
    <script type="text/javascript" src="../js/common.js"></script>
</head>
<body>
<!-- 页面加载loading -->
<div class="page-loading">
    <div class="ball-loader">
        <span></span><span></span><span></span><span></span>
    </div>
</div>
<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 表格工具栏 -->
            <form class="layui-form toolbar">
                <div class="layui-form-item">
                    <!--                    <div class="layui-inline">-->
                    <!--                        <label class="layui-form-label">市:</label>-->
                    <!--                        <div class="layui-input-inline">-->
                    <!--                            <input name="city" class="layui-input" placeholder="请输入市"/>-->
                    <!--                        </div>-->
                    <!--                    </div>-->
                    <!--                    <div class="layui-inline">-->
                    <!--                        <label class="layui-form-label">区:</label>-->
                    <!--                        <div class="layui-input-inline">-->
                    <!--                            <input name="region" class="layui-input" placeholder="请输入区"/>-->
                    <!--                        </div>-->
                    <!--                    </div>-->
                    <!--                    <div class="layui-inline">-->
                    <!--                        <label class="layui-form-label">县:</label>-->
                    <!--                        <div class="layui-input-inline">-->
                    <!--                            <input name="county" class="layui-input" placeholder="请输入县"/>-->
                    <!--                        </div>-->
                    <!--                    </div>-->
                    <div class="layui-inline">
                        <label class="layui-form-label">单位名:</label>
                        <div class="layui-input-inline">
                            <input name="name" class="layui-input" placeholder="请输入单位名"/>
                        </div>
                    </div>
                    <!--                    <div class="layui-inline">-->
                    <!--                        <label class="layui-form-label">状态:</label>-->
                    <!--                        <div class="layui-input-inline">-->
                    <!--                            <input name="state" class="layui-input" placeholder="请输入状态"/>-->
                    <!--                        </div>-->
                    <!--                    </div>-->
                    <div class="layui-inline">&emsp;
                        <button class="layui-btn icon-btn" lay-filter="searchBtn" lay-submit>
                            <i class="layui-icon">&#xe615;</i>搜索
                        </button>&nbsp;
                        <button shiro:hasPermission="company:export" id="exportBtn" class="layui-btn icon-btn"
                                type="button">
                            <i class="layui-icon">&#xe67d;</i>导出
                        </button>
                    </div>
                </div>
            </form>
            <!-- 数据表格 -->
            <table id="dataTable" lay-filter="dataTable"></table>
        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="tableBar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<script type="text/html" id="userStatus">
    <input type="checkbox" lay-filter="editStateBtn" value="{{d.id}}" lay-skin="switch"
           lay-text="正常|锁定" {{d.state==0?'checked':''}} style="display: none;"/>
    <p style="display: none;">{{d.state == 0 ? '正常' : '锁定'}}</p>
</script>

<!-- 表单弹窗 -->
<script type="text/html" id="formDialog">
    <form id="dataForm" lay-filter="dataForm" class="layui-form model-form">
        <input name="id" type="hidden"/>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">市:</label>
            <div class="layui-input-block">
                <input name="city" placeholder="请输入市" class="layui-input"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">区:</label>
            <div class="layui-input-block">
                <input name="region" placeholder="请输入区" class="layui-input"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">县:</label>
            <div class="layui-input-block">
                <input name="county" placeholder="请输入县" class="layui-input"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">单位名:</label>
            <div class="layui-input-block">
                <input name="name" id="name" placeholder="请输入单位名" class="layui-input"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>
        <!--        <div class="layui-form-item">-->
        <!--            <label class="layui-form-label layui-form-required">创建时间:</label>-->
        <!--            <div class="layui-input-block">-->
        <!--                <input id="creDate" name="creDate" placeholder="请选择创建时间"-->
        <!--                       class="layui-input icon-date" autocomplete="off" lay-verType="tips"-->
        <!--                       lay-verify="required" required/>-->
        <!--            </div>-->
        <!--        </div>-->
        <!--        <div class="layui-form-item">-->
        <!--            <label class="layui-form-label layui-form-required">修改时间:</label>-->
        <!--            <div class="layui-input-block">-->
        <!--                <input id="updDate" name="updDate" placeholder="请选择修改时间"-->
        <!--                       class="layui-input icon-date" autocomplete="off" lay-verType="tips"-->
        <!--                       lay-verify="required" required/>-->
        <!--            </div>-->
        <!--        </div>-->
        <!--        <div class="layui-form-item">-->
        <!--            <label class="layui-form-label layui-form-required">状态:</label>-->
        <!--            <div class="layui-input-block">-->
        <!--                <input name="state" placeholder="请输入状态" class="layui-input"-->
        <!--                       lay-verType="tips" lay-verify="required" required/>-->
        <!--            </div>-->
        <!--        </div>-->

        <div class="layui-form-item text-right">
            <button class="layui-btn" lay-filter="saveBtn" lay-submit>保存</button>
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
        </div>
    </form>
</script>
<!-- js部分 -->
<!--<div th:replace="common/scripts :: common"></div>-->
<!--<div th:replace="common/scripts :: custom"></div>-->
<!--<div>-->
<!--    <script>-->
<!--        //全局变量 Shiro权限-->
<!--        var shiroSave = false;-->
<!--        var shiroDelete = false;-->
<!--        var shiroUpdate = false;-->
<!--        var shiroInfo = false;-->
<!--    </script>-->
<!--    &lt;!&ndash; 设置Shiro权限变量 &ndash;&gt;-->
<!--    <shiro:hasAllPermissions name="company:save">-->
<!--        <script>shiroSave = true;</script>-->
<!--    </shiro:hasAllPermissions>-->
<!--    <shiro:hasAllPermissions name="company:delete">-->
<!--        <script>shiroDelete = true;</script>-->
<!--    </shiro:hasAllPermissions>-->
<!--    <shiro:hasAllPermissions name="company:update">-->
<!--        <script>shiroUpdate = true;</script>-->
<!--    </shiro:hasAllPermissions>-->
<!--</div>-->
<script>
    layui.use(['layer', 'form', 'table', 'util', 'laydate', 'tableX', 'admin'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var util = layui.util;
        var laydate = layui.laydate;
        var tableX = layui.tableX;
        var admin = layui.admin;

        /* 渲染表格 */
        var cols = [
            {type: 'checkbox'},
            {field: 'city', title: '市', align: 'center'},
            {field: 'region', title: '区', align: 'center'},
            {field: 'county', title: '县', align: 'center'},
            {field: 'name', title: '单位名', align: 'center'},
            {field: 'state', title: '状态', templet: '#userStatus', sort: true, width: 100},
            {title: '操作', toolbar: '#tableBar', align: 'center', minWidth: 200}
        ];

        var insTb = table.render({
            elem: '#dataTable',
            url: '/company/selCompanyPageAfter',
            page: true,
            toolbar: [shiroToolbar(true, true)].join(''),
            cellMinWidth: 100,
            cols: [cols],
            method: 'post',
            dataType: 'json',
            contentType: 'application/json',
            headers: {"X-Token": C.find("X-Token")},
            parseData: function (res) { //res 即为原始返回的数据
                return {
                    "code": res.errorCode, //解析接口状态
                    "msg": res.message, //解析提示文本
                    "count": res.body.total, //解析数据长度
                    "data": res.body.records //解析数据列表
                };
            },
            request: {
                pageName: 'pageIndex' //页码的参数名称，默认：page
                ,
                limitName: 'pageSize' //每页数据量的参数名，默认：limit
            },
            done: function () {
                // 绑定鼠标右键
                tableX.bindCtxMenu('dataTable', shiroBindCtxMenu(true, true));
            }
        });

        /* 表格工具条点击事件 */
        table.on('tool(dataTable)', function (obj) {
            if (obj.event === 'edit') { // 修改
                showEditModel(obj.data);
            } else if (obj.event === 'del') { // 删除
                doDel(obj.data);
            } else if (obj.event === 'reset') { // 重置密码
                resetPsw(obj);
            }
        });

        /* 表格头工具栏点击事件 */
        table.on('toolbar(dataTable)', function (obj) {
            if (obj.event === 'add') { // 添加
                showEditModel();
            } else if (obj.event === 'del') { // 删除
                var checkRows = table.checkStatus('dataTable');
                if (checkRows.data.length === 0) {
                    layer.msg('请选择要删除的数据', {icon: 2});
                    return;
                }
                var ids = checkRows.data.map(function (d) {
                    return d.id;
                });
                doDel({ids: ids});
            }
        });

        form.on('switch(editStateBtn)', function (obj) {

            var loadIndex = layer.load(2);
            var data = {
                "id": obj.elem.value,
                "state": obj.elem.checked ? 0 : 1
            }
            $.ajax({
                url: '/company/update',
                type: 'post',
                data: JSON.stringify(data),
                dataType: 'JSON',
                contentType: 'application/json',
                beforeSend: function (xhr) {
                    xhr.setRequestHeader("X-Token", C.find("X-Token"));
                },
                success: function (res) {
                    if (res.message == 'success') {
                        layer.msg('操作成功', {icon: 1})
                        layer.close(loadIndex);
                        // show();
                        // insTb.reload({page: {curr: 1}});
                        // ;
                    } else {
                        layer.msg(res.message, {icon: 2});
                        $(obj.elem).prop('checked', !obj.elem.checked);
                        form.render('checkbox');
                        // layer.msg(res.message);
                    }
                    //     if (res.code == 0) {
                    //     layer.close(dIndex);
                    //     layer.msg(res.msg, {icon: 1});
                    //     insTb.reload({page: {curr: 1}});
                    // } else {
                    //     layer.msg(res.msg, {icon: 2});
                    // }
                }
            })
            // $.post('user/status', {
            //     id: obj.elem.value,
            //     status: obj.elem.checked ? 0 : 1
            // }, function (res) {
            //     layer.close(loadIndex);
            //     if (res.code === 0) {
            //         layer.msg(res.msg, {icon: 1});
            //     } else {
            //         layer.msg(res.msg, {icon: 2});
            //         $(obj.elem).prop('checked', !obj.elem.checked);
            //         form.render('checkbox');
            //     }
            // }, 'json');
        });

        // 显示编辑弹窗
        function showEditModel(mData) {
            admin.open({
                type: 1,
                area: '800px',
                title: (mData ? '修改' : '添加'),
                content: $('#formDialog').html(),
                shadeClose: true,
                success: function (layero, dIndex) {
                    form.val('dataForm', mData);  // 回显数据
                    form.on('submit(saveBtn)', function (data) {
                        // $.post(mData ? 'company/update' : 'company/save', data.field, function (res) {
                        //     if (res.code == 0) {
                        //         layer.close(dIndex);
                        //         layer.msg(res.msg, {icon: 1});
                        //         insTb.reload({page: {curr: 1}});
                        //     } else {
                        //         layer.msg(res.msg, {icon: 2});
                        //     }
                        // }, 'json');
                        var city = $("[name='city']").val();
                        var region = $("[name='region']").val();
                        var county = $("[name='county']").val();
                        var name = $("#name").val();

                        if (mData) {
                            var data = {
                                "city": city,
                                "region": region,
                                "county": county,
                                "name": name,
                                "id": mData.id,
                            }
                            $.ajax({
                                url: '/company/update',
                                type: 'post',
                                data: JSON.stringify(data),
                                dataType: 'JSON',
                                contentType: 'application/json',
                                beforeSend: function (xhr) {
                                    xhr.setRequestHeader("X-Token", C.find("X-Token"));
                                },
                                success: function (res) {
                                    if (res.message == 'success') {
                                        layer.msg('修改成功', {icon: 1})
                                        layer.close(dIndex);
                                        // show();
                                        insTb.reload({page: {curr: 1}});
                                        ;
                                    } else {
                                        layer.msg(res.message);
                                    }
                                    //     if (res.code == 0) {
                                    //     layer.close(dIndex);
                                    //     layer.msg(res.msg, {icon: 1});
                                    //     insTb.reload({page: {curr: 1}});
                                    // } else {
                                    //     layer.msg(res.msg, {icon: 2});
                                    // }
                                }
                            })
                        } else {
                            var data = {
                                "city": city,
                                "region": region,
                                "county": county,
                                "name": name
                            }

                            $.ajax({
                                url: '/company/insert',
                                type: 'post',
                                data: JSON.stringify(data),
                                dataType: 'JSON',
                                contentType: 'application/json',
                                beforeSend: function (xhr) {
                                    xhr.setRequestHeader("X-Token", C.find("X-Token"));
                                },
                                success: function (res) {
                                    if (res.message == 'success') {
                                        layer.msg('添加成功', {icon: 1})
                                        layer.close(dIndex);
                                        // show();
                                        insTb.reload({page: {curr: 1}});
                                        ;
                                    } else {
                                        layer.msg(res.message);
                                    }
                                    //     if (res.code == 0) {
                                    //     layer.close(dIndex);
                                    //     layer.msg(res.msg, {icon: 1});
                                    //     insTb.reload({page: {curr: 1}});
                                    // } else {
                                    //     layer.msg(res.msg, {icon: 2});
                                    // }
                                }
                            })
                        }

                        return false;
                    });
                }
            });

            /* 渲染laydate */
            laydate.render({
                elem: '#creDate',
            });
            /* 渲染laydate */
            laydate.render({
                elem: '#updDate',
            });
        }

        /* 表格搜索 */
        form.on('submit(searchBtn)', function (data) {
            if (data.field.dateRange) {
                var searchDate = data.field.dateRange.split(' - ');
                data.field.startDate = searchDate[0];
                data.field.endDate = searchDate[1];
            } else {
                data.field.startDate = '';
                data.field.endDate = '';
            }
            data.field.dateRange = undefined;
            insTb.reload({where: data.field, page: {curr: 1}});
            return false;
        });

        /* 导出excel */
        $('#exportBtn').click(function () {
            var checkRows = table.checkStatus('dataTable');
            if (checkRows.data.length === 0) {
                layer.msg('请选择要导出的数据', {icon: 2});
            } else {
                tableX.exportDataX({
                    cols: insTb.config.cols,
                    data: checkRows.data,
                    fileName: '导出数据'
                });
            }
        });


        /* 删除 */
        window.doDel = function (obj) {

            layer.confirm('确定要删除选中数据吗？', {
                skin: 'layui-layer-admin',
                shade: .1
            }, function (i) {
                layer.close(i);
                var loadIndex = layer.load(2);
                $.ajax({
                    url: '/company/delete?id='+obj.id,
                    type: 'post',
                    // data: {"id": obj.id},
                    dataType: 'JSON',
                    contentType: 'application/json',
                    beforeSend: function (xhr) {
                        xhr.setRequestHeader("X-Token", C.find("X-Token"));
                    },
                    success: function (res) {
                        if (res.message == 'success') {
                            layer.msg('删除成功', {icon: 1})

                            // show();
                            insTb.reload({page: {curr: 1}});
                            layer.close(loadIndex);

                        } else {
                            layer.msg(res.message);
                        }

                    }
                })
            });
        }

        /* 渲染时间选择 */
        laydate.render({
            elem: 'input[name="dateRange"]',
            type: 'date',
            range: true,
            trigger: 'click'
        });
    });
</script>
</body>
</html>